package com.zhangxx.inventory.thread.request;

import com.google.common.collect.Lists;
import com.zhangxx.inventory.thread.threadpool.ProcessThreadPool;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description : 请求队列
 * @Author : leizhang
 * @Date 13:56 2022/3/18
 **/
public class RequestQueue {

    /**
     * @Description : 内存队列
     **/
    private List<ArrayBlockingQueue<Request>> queues = Lists.newArrayList();

    /**
     * @Description : 标识位,全局判断对redis读写请求的顺序
     **/
    private Map<String,Boolean> flagMap = new ConcurrentHashMap<String,Boolean>();

    /**单例创建对象**/
    private static class Singleton{
        private static volatile RequestQueue instance;

        private static RequestQueue getInstance() {
            if (null == instance) {
                synchronized (ProcessThreadPool.class){
                    if (null == instance) {
                        instance = new RequestQueue();
                    }
                }
            }
            return instance;
        }
    }

    public static RequestQueue getInstance(){
        return Singleton.getInstance();
    }

    public void addQueue(ArrayBlockingQueue<Request> queue){
        this.queues.add(queue);
    }

    public int queueSize(){
        return queues.size();
    }

    /**
     * @Description : 获取队列
     * @Author : leizhang
     **/
    public ArrayBlockingQueue<Request> getQueue(int index){
        return queues.get(index);
    }

    public Map<String,Boolean> getFlagMap(){
        return flagMap;
    }
}
